namespace SimpleWebServices.Web.Routing
{
    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Mvc;
    using Xunit;

    public sealed class TenantRouteOfTFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantRoute<TenantForm>>()
                .DerivesFrom<RouteBase>()
                .IsConcreteClass()
                .IsSealed()
                .NoDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor_Type_RouteName()
        {
            Assert.NotNull(new TenantRoute<TenantForm>(typeof(Controller), new RouteName("name")));
        }

        [Fact]
        public void ctor_TypeInvalid_RouteName()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => new TenantRoute<TenantForm>(typeof(string), new RouteName("name")));
        }

        [Fact]
        public void ctor_TypeNull_RouteName()
        {
            Assert.Throws<ArgumentNullException>(() => new TenantRoute<TenantForm>(null as Type, new RouteName("name")));
        }

        [Fact]
        public void ctor_Type_RouteNameNull()
        {
            Assert.Throws<ArgumentNullException>(() => new TenantRoute<TenantForm>(typeof(Controller), null as RouteName));
        }

        [Fact]
        public void op_GetRouteData_HttpContextBaseNull()
        {
            Assert.Throws<ArgumentNullException>(() => new TenantRoute<TenantForm>(typeof(Controller), "name").GetRouteData(null));
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_whenPost()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantForm>(new
                {
                    urn = "urn://example.com/form/test",
                    xml = "<container><form /></container>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.AppRelativeCurrentExecutionFilePath).Returns("~/sample/test/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.HttpMethod).Returns("POST").Verifiable();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.PathInfo).Returns(string.Empty).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new TenantRoute<TenantForm>(typeof(TenantFormController), "form").GetRouteData(mock.Object);

                Assert.Equal<string>("sample/test/form", (actual.Route as Route).Url);
                Assert.Equal<string>("TenantForm", (string)actual.Values["controller"]);
                Assert.Equal<string>("Post", (string)actual.Values["action"]);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_whenLanneg()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantForm>(new
                {
                    urn = "urn://example.com/form/test",
                    xml = "<container><form /></container>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.AppRelativeCurrentExecutionFilePath).Returns("~/sample/test/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.PathInfo).Returns(string.Empty).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new TenantRoute<TenantForm>(typeof(TenantFormController), "form").GetRouteData(mock.Object);

                Assert.Equal<string>("sample/test/form", (actual.Route as Route).Url);
                Assert.Equal<string>("TenantForm", (string)actual.Values["controller"]);
                Assert.Equal<string>("LanguageNegotiation", (string)actual.Values["action"]);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_whenConneg()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantForm>(new
                {
                    urn = "urn://example.com/form/test",
                    xml = "<container><form /></container>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.AppRelativeCurrentExecutionFilePath).Returns("~/sample/test/form.en").Verifiable();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form.en").Verifiable();
                mock.SetupGet<string>(x => x.Request.PathInfo).Returns(string.Empty).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new TenantRoute<TenantForm>(typeof(TenantFormController), "form").GetRouteData(mock.Object);

                Assert.Equal<string>("sample/test/form.en", (actual.Route as Route).Url);
                Assert.Equal<string>("TenantForm", (string)actual.Values["controller"]);
                Assert.Equal<string>("ContentNegotiation", (string)actual.Values["action"]);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_whenHtml()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantForm>(new
                {
                    urn = "urn://example.com/form/test",
                    xml = "<container><form /></container>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.AppRelativeCurrentExecutionFilePath).Returns("~/sample/test/form.en.html").Verifiable();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form.en.html").Verifiable();
                mock.SetupGet<string>(x => x.Request.PathInfo).Returns(string.Empty).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new TenantRoute<TenantForm>(typeof(TenantFormController), "form").GetRouteData(mock.Object);

                Assert.Equal<string>("sample/test/form.en.html", (actual.Route as Route).Url);
                Assert.Equal<string>("TenantForm", (string)actual.Values["controller"]);
                Assert.Equal<string>("HtmlRepresentation", (string)actual.Values["action"]);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_whenRoot()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>();

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new TenantRoute<TenantForm>(typeof(Controller), "example").GetRouteData(mock.Object);

                Assert.Null(actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_whenNestedPost()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantForm>(new
                {
                    urn = "urn://example.com/form/abc/xyz",
                    xml = "<container><form /></container>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.AppRelativeCurrentExecutionFilePath).Returns("~/sample/abc/xyz/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.HttpMethod).Returns("POST").Verifiable();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/abc/xyz/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.PathInfo).Returns(string.Empty).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new TenantRoute<TenantForm>(typeof(TenantFormController), "form").GetRouteData(mock.Object);

                Assert.Equal<string>("sample/abc/xyz/form", (actual.Route as Route).Url);
                Assert.Equal<string>("TenantForm", (string)actual.Values["controller"]);
                Assert.Equal<string>("Post", (string)actual.Values["action"]);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetVirtualPath_RequestContextNull_RouteValueDictionary()
        {
            Assert.Throws<ArgumentNullException>(() => new TenantRoute<TenantForm>(typeof(Controller), "example").GetVirtualPath(null, new RouteValueDictionary()));
        }

        [Fact]
        public void op_GetVirtualPath_RequestContext_RouteValueDictionary()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantForm>(new
                {
                    urn = "urn://example.com/form/test",
                    xml = "<container><form /></container>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RequestContext request = new RequestContext(mock.Object, new RouteData());

                VirtualPathData actual = new TenantRoute<TenantForm>(typeof(Controller), "form").GetVirtualPath(request, new RouteValueDictionary());

                Assert.NotNull(actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetVirtualPath_RequestContext_RouteValueDictionary_whenRoot()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>();

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RequestContext request = new RequestContext(mock.Object, new RouteData());

                VirtualPathData actual = new TenantRoute<TenantForm>(typeof(Controller), "example").GetVirtualPath(request, new RouteValueDictionary());

                Assert.Null(actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}